import time

from rest_framework import serializers, exceptions

from user_app import models
from user_app.models import User
from article_app.models import Article
from utils.regexp_rule import EMAIL, PASSWORD, NICK_NAME
from utils.time_zom import time2chinese
from utils.encode_decode import my_encode
from utils.encode_decode import ShortLink
# 猴子补丁的写法
my_encode = ShortLink().encrypt


class UserSimpleSerializer(serializers.ModelSerializer):

    uid = serializers.SerializerMethodField()
    register_time = serializers.SerializerMethodField()
    last_login_time = serializers.SerializerMethodField()
    article_no = serializers.SerializerMethodField()
    fans_no = serializers.SerializerMethodField()
    agree_no = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()
    role = serializers.SerializerMethodField()
    is_valid = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = ('uid', 'nick_name', 'tags', 'role', 'email', 'info', 'level', 'avatar', 'register_time', 'last_login_time', 'fans_no', 'article_no', 'agree_no', 'level', 'is_delete', 'is_valid')


    def get_uid(self, obj):
        return my_encode(obj.id)
    
    def get_tags(self, obj):
        return obj.tags.split('|')
    
    def get_role(self, obj):
        return "普通"
    
    def get_is_valid(self, obj):
        return False if obj.is_valid else True
    
    def get_register_time(self, obj):
        time_ = obj.register_time
        day_time = time2chinese(time_)
        return day_time
    
    def get_last_login_time(self, obj):
        time_ = obj.last_login_time
        day_time = time2chinese(time_)
        return day_time

    def get_article_no(self, obj):
        return Article.objects.filter(u_id=obj.id, is_delete=0).count()
    
    def get_fans_no(self, obj):
        return 0
    
    def get_agree_no(self, obj):
        return 0


class ArticleSimpleSerializer(serializers.ModelSerializer):

    aid = serializers.SerializerMethodField()
    uid = serializers.SerializerMethodField()
    content = serializers.SerializerMethodField()
    nick_name = serializers.SerializerMethodField()
    add_time = serializers.SerializerMethodField()
    change_time = serializers.SerializerMethodField()
    is_recommend = serializers.SerializerMethodField()
    tags = serializers.SerializerMethodField()


    class Meta:
        model = Article
        fields = ('aid', 'uid', 'nick_name', 'title', 'head_avatar', 'content', 'add_time', 'mode', 'change_time', 'is_check', 'is_delete', 'is_recommend', 'recommend', 'tags')

    def get_content(self, obj):
        return obj.content_text
    
    def get_is_recommend(self, obj):
        return True if obj.is_recommend else False
    
    def get_aid(self, obj):
        return my_encode(obj.id)
    
    def get_uid(self, obj):
        return my_encode(obj.u_id.id)
    
    def get_nick_name(self, obj):
        return obj.u_id.nick_name
    
    def get_add_time(self, obj):
        time_ = obj.add_time
        day_time = time2chinese(time_)
        return day_time
    
    def get_change_time(self, obj):
        time_ = obj.change_time
        day_time = time2chinese(time_)
        return day_time
    
    def get_tags(self, obj):
        return obj.tags.split('|')
    